#pragma once

#include "Handler.h"
#include "Define.h"

namespace wsbus {

	class NodeImpl;
	class CUBEBOTBUS_API Node
	{
	public:
		Node(int appId, const std::string &host, const std::string &port);
		~Node();

		void setAuth(const std::string &username, const std::string &password);
		void start();

		PackagePtr newRequestPackage(const MessagePtr &msg, int toAppType, int toSessionId = 0);
		PackagePtr newRequestPackage(const std::string &command, const std::string &data, int toAppType, int toSessionId = 0);
		int sendMessage(const PackagePtr &reqMsg, PackagePtr &rspMsg, int msTimeout = 10000);
		int postMessage(const PackagePtr &msg, const HandleMessage &cb, int msTimeout = 10000);
		int pushMessage(const MessagePtr &msg);
		int replyMessage(const PackagePtr &reqMsg, const MoreMessagePtr &rspMsg);

		void addRequestHandler(const std::string &cmd, const std::function<MoreMessagePtr(const PackagePtr&)> &cb);
		void addPublishHandler(const std::string &cmd, const std::function<void(const PackagePtr)> &cb);
		void setStatusChangedHandler(const std::function<void(int status)> &cb);

	private:
		NodeImpl *impl_;
	};

}